﻿//  Chris Pembroke  x00115839
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace agecalcMiniProjMethod
{
    public class DevelopMethod //  to develop a method that can be used in the miniproject to determine a childs age
    {

        public static double AgeCalc(DateTime dateOfBirth, DateTime dateOfReadings)
        {   
            // use of time span which is a struct 
            TimeSpan diffInDaysHours = dateOfReadings.Subtract(dateOfBirth);  // subtracting two datetime types leaves a timespan which is just days hours minutes seconds
            DateTime diffYearsMonthsDays = DateTime.MinValue + diffInDaysHours;  // need to return to datetime type by adding datetime min value which is 01 year 01 month 01 days 
            double ageYears = diffYearsMonthsDays.Year - 1; // from the years months days take the years and substract 1 as it had been added in the timespan to datetime conversion    
            int ageMonths = diffYearsMonthsDays.Month - 1; // from the years months days just take the months substract 1 as it had been added in the timespan to datetime conversion 
           
            // need to round the month up or down 
            if ((ageMonths > 3) && (ageMonths < 10))   // if months are 4,5,6,7,8,9
            {
                ageYears = ageYears + 0.5;
            }
            if ((ageMonths > 9) && (ageMonths < 12))   // if months are 10,11,
            {
                ageYears = ageYears + 1;
            }
                                                       // if month 0,1,2,3 round down to nearest year ie. just leave age as is 
            return ageYears;
        }

        static void Main()
        {
            Console.WriteLine("METHOD TO CALCULATE AGE FROM DIFFERENCE OF DATE OF BIRTH AND A READING DATE");
            Console.WriteLine();
            try
            {
                bool anotherCalc = true;
                while (anotherCalc == true)
                {

                    Console.Write("Enter Child Date of Birth eg, 1 feb 2013:- ");
                    DateTime dob = DateTime.Parse(Console.ReadLine());

                    Console.Write("Enter reading Date eg, 1 feb 2013:- ");
                    DateTime dor = DateTime.Parse(Console.ReadLine());

                    Console.WriteLine("Childs age is {0}", AgeCalc(dob, dor));

                    Console.WriteLine();
                    Console.Write("To run another age check type 'y':- ");
                    string s = Console.ReadLine().ToLower();
                    if( s != "y")
                    {
                        anotherCalc = false;
                    }

                }
            } // close of try block
            catch(Exception e)
            {
                Console.WriteLine("Caught an Exception! {0}", e.Message);
            } // close of catch block

            Console.WriteLine();
            Console.Write("Press ENTER to Exit!");
            Console.ReadKey();
        } // close of main method

    } // close class DevelopMethod

} // close namespace
